#region Summary

// 
// Owner : JackieHan 
// 
// Solution : ResearchTest 
// 
// Project : JackieHan.ConfOrm.Test
// 
// File : NhConfig.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : NHibernate配置
// 
// History : 
//			2011-06-28 15:41:52	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using NHibernate.ByteCode.Castle;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Cfg.MappingSchema;
using NHibernate.Dialect;
using NHibernate.Driver;

#endregion

namespace JackieHan.ConfOrm.Test
{
    ///<summary>
    ///NHibernate配置
    ///</summary>
    public class NhConfig
    {
        public static Configuration ConfigureNhiberate()
        {
            var config = new Configuration();
            config.SessionFactoryName("personal");
            config.Proxy(p =>
                             {
                                 p.Validation = false;
                                 p.ProxyFactoryFactory<ProxyFactoryFactory>();
                             });
            config.DataBaseIntegration(db =>
                                           {
                                               db.Dialect<MsSql2008Dialect>();
                                               db.Driver<SqlClientDriver>();
                                               db.IsolationLevel = IsolationLevel.ReadCommitted;
                                               db.ConnectionStringName = "confOrmTest";
                                               db.Timeout = 10;
                                               db.HqlToSqlSubstitutions = "true 1,false 0,yes 'Y',no 'N'";
                                               db.LogSqlInConsole = true;
                                               db.LogFormatedSql = true;
                                           });
            return config;
        }

        public static string Serializer(HbmMapping hbmElement)
        {
            var setting = new XmlWriterSettings { Indent = true };
            var serializer = new XmlSerializer(typeof(HbmMapping));
            using (var memStream = new MemoryStream())
            using (var xmlWriter = XmlWriter.Create(memStream, setting))
            {
                serializer.Serialize(xmlWriter, hbmElement);
                memStream.Flush();
                memStream.Position = 0;
                var sr = new StreamReader(memStream);
                return sr.ReadToEnd();
            }
        }
    }
}